package com.groupbuying.ordosmarathon.marathonOrder;

import com.groupbuying.ordosmarathon.context.FilterContextHandler;
import com.groupbuying.ordosmarathon.util.PageUtils;
import com.groupbuying.ordosmarathon.util.Query;
import com.groupbuying.ordosmarathon.util.apiresult.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Vincentxin
 * @Date 2019-08-05
 */
@RestController
@RequestMapping()
public class MarathonOrderController {
    private static Logger log = LoggerFactory.getLogger(MarathonOrderController.class);

    @Value("${counterinfo.counterId1}")
    private String counterId1; //彩虹滑梯

    @Value("${counterinfo.counterId2}")
    private String counterId2; // 娃娃机
    @Value("${counterinfo.counterId3}")
    private String counterId3;//线下付款收银台
    @Value("${counterinfo.wawaPrice}")
    private String wawa;
    @Value("${counterinfo.caihongPrice}")
    private String caihong;
    @Autowired
    private MarathonOrderDao marathonOrderDao;

    /**
     * 查询马拉松用户订单
     * @param requestParam
     * @return
     */
    @GetMapping("/orderList")
    public ApiResult orderList(Map<String,Object> requestParam){
        log.info("请求订单参数：{}",requestParam);
        String userId = FilterContextHandler.getUserID();
        log.info("通过Token获取用户ID：{}",userId);

        requestParam.put("customerId",userId);
        requestParam.put("isDelete","1");
        requestParam.put("orderState","1");
        String[] counterIds = {counterId1,counterId2};
        requestParam.put("counterIds",counterIds);//固定的收银台ID
        log.info("发起请求查询的参数：{}",requestParam);
        Query query =new Query(requestParam);
        PageUtils pageUtils = null;
        try{

            pageUtils = new PageUtils(marathonOrderDao.orderList(query),marathonOrderDao.count(query));
        }catch (Exception e){
            log.error(e.getMessage());
        }
        log.info("查询回执信息:{}",pageUtils);
        return ApiResult.success(pageUtils);
    }

    /**
     * 查询马拉松用户的票币的记录
     * @return
     */
    @GetMapping("/getTicket")
    public ApiResult getTicket(){
        String userId = FilterContextHandler.getUserID();
        Map<String,Object> param = new HashMap<>();
        param.put("customerId",userId);
        param.put("isDelete","1");//未删除
        param.put("orderState","1");//订单支付成功
        param.put("dealState","0");//订单交易状态是在进行中
        String[] counterIds = {counterId1,counterId2};
        param.put("counterIds",counterIds);//固定的收银台ID
        List<Map<String,Object>> result = marathonOrderDao.getTicket(param);
        if (result == null || result.size() > 2){
            return ApiResult.failure("查询错误");
        }
        Map<String,String> returnDate =  new HashMap<>();
        if (result.size()==0){
            returnDate.put("caihong","0");
            returnDate.put("wawa","0");
            return ApiResult.success(returnDate);
        }
        if (result.size() ==1){
            Map<String,Object> map =  result.get(0);
            if (map.get("counterId").equals(counterId1)){
                returnDate.put("caihong",new BigDecimal(map.get("price").toString()).divide(new BigDecimal(caihong),0,BigDecimal.ROUND_DOWN).toString());
                returnDate.put("wawa","0");
            }else if (map.get("counterId").equals(counterId2)){
                returnDate.put("caihong","0");
                returnDate.put("wawa",new BigDecimal(map.get("price").toString()).divide(new BigDecimal(wawa),0,BigDecimal.ROUND_DOWN).toString());
            }
        }
        for (Map<String,Object> map:result){
            if (map.get("counterId").equals(counterId1)){
               returnDate.put("caihong",new BigDecimal(map.get("price").toString()).divide(new BigDecimal(caihong),0,BigDecimal.ROUND_DOWN).toString());
            }else if (map.get("counterId").equals(counterId2)){
                returnDate.put("wawa",new BigDecimal(map.get("price").toString()).divide(new BigDecimal(wawa),0,BigDecimal.ROUND_DOWN).toString());
            }
        }
        return ApiResult.success(returnDate);
    }

    /**
     * 查询马拉松所有订单和金额
     * @return
     */
    @GetMapping("/getCountAndTotalPrice")
    public ApiResult getCountAndTotalPrice(){
        Map<String,Object> param = new HashMap<>();
        param.put("isDelete","1");//未删除
        param.put("orderState","1");//订单支付成功
        param.put("dealState","0");//订单交易状态是在进行中
        String[] counterIds = {counterId1,counterId2,counterId3};
        param.put("counterIds",counterIds);//固定的收银台ID
        //给个时间段统计
        param.put("endTime",new Date());
        param.put("startTime","2019-08-23 09:30:00");
        List<Map<String,Object>> result = marathonOrderDao.totalCountAndTotalPrice(param);
        if (result == null){
            return ApiResult.failure("查询失败");
        }
        Map<String,String> returnDate =  new HashMap<>();
        String caihongOrders = "0";
        String caihongPrice = "0.00";
        String wawaOrders = "0";
        String wawaPrice = "0.00";
        String counterOrders = "0";
        String counterPrice = "0.00";
        for (Map<String,Object> map:result){
            if (counterId1.equals(map.get("counterId"))){
                caihongOrders = map.get("orderCount").toString();
                caihongPrice = map.get("totalPrice").toString();
            }else if (counterId2.equals(map.get("counterId"))){
                wawaOrders = map.get("orderCount").toString();
                wawaPrice  = map.get("totalPrice").toString();
            }else if (counterId3.equals(map.get("counterId"))){
                counterOrders = map.get("orderCount").toString();
                counterPrice = map.get("totalPrice").toString();
            }
        }
        returnDate.put("caihongOrders",caihongOrders);
        returnDate.put("caihongPrice",caihongPrice);
        returnDate.put("wawaOrders",wawaOrders);
        returnDate.put("wawaPrice",wawaPrice);
        returnDate.put("counterOrders",counterOrders);
        returnDate.put("counterPrice",counterPrice);
        returnDate.put("totalCount",new BigDecimal(caihongOrders)
                .add(new BigDecimal(wawaOrders))
                .add(new BigDecimal(counterOrders)).toString());
        returnDate.put("totalPrice",new BigDecimal(caihongPrice)
                .add(new BigDecimal(wawaPrice))
                .add(new BigDecimal(counterPrice)).toString());
        return ApiResult.success(returnDate);
    }


}
